package com.ztesoft.sca.controller;

import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.ImButtonDto;
import com.ztesoft.sca.service.ImButtonService;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;
import com.ztesoft.sca.util.seq.SequenceCreator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/*
 * hxz 按钮操作类
 * */

@RestController
public class ImButtonController {
	private static final Logger logger = Logger.getLogger(ImButtonController.class);
    
    //private ImButtonDao imButtonDao;
	@Autowired
    private ImButtonService ibService;
	
    /*
     * hxz 按钮查询
     * */
	@RequestMapping(value = "/imbutton/list",method = {RequestMethod.GET})
	public String imButtonList(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imbutton--list-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		int from=qryJson.getInt("page");//第1页开始
		int size=qryJson.getInt("rownum"); //每页大小
		Long buttonId=qryJson.getLong("buttonId");
		String buttonCode=qryJson.getString("buttonCode");
		String buttonName=qryJson.getString("buttonName");
		
		ImButtonDto ibDto=new ImButtonDto();
		ibDto.setButtonId(buttonId);
		ibDto.setButtonCode(buttonCode);
		ibDto.setButtonName(buttonName);
		
		List<ImButtonDto> list = new ArrayList<>();
		try {
			list = this.ibService.listImButton(ibDto, from, size);
		} catch (Exception e) {
			 logger.error(" ImButtonList occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(ImButtonDto pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("buttonId", pLog.getButtonId());
            jo.put("buttonCode", pLog.getButtonCode());
            jo.put("buttonName", pLog.getButtonName());
            jo.put("comments", pLog.getComments());
            
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", from);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
	
	/*
	 * hxz 按钮新增
	 * */
	@RequestMapping(value = "/imbutton/add",method = {RequestMethod.POST})
	public String imButtonAdd(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imbutton--add-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        long buttonId = 0L;
        try {
        	buttonId = SequenceCreator.getSequence("IM_BUTTON_SEQ");
       } catch (Exception e) {
           logger.error("sequenceCreator obtain IM_BUTTON occur an error!",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       String buttonCode = json.getString("buttonCode");
       String buttonName = json.getString("buttonName");
       String comments = json.getString("comments");
       
      
       
       ImButtonDto ibDto = new ImButtonDto();
       ibDto.setButtonId(buttonId);
       ibDto.setButtonName(buttonName);
       ibDto.setButtonCode(buttonCode);
       ibDto.setComments(comments);
       
       try {
           this.ibService.insertImButton(ibDto);
       } catch (Exception e) {
           logger.error("create IM_BUTTON occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,ibDto);
	}
		
	/*
	 * hxz 按钮修改
	 * */
	@RequestMapping(value = "/imbutton/modify",method = {RequestMethod.POST})
	public String imButtonModify(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imbutton--modify-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        
       Long buttonId = json.getLong("buttonId");
       String buttonCode = json.getString("buttonCode");
       String buttonName = json.getString("buttonName");
       String comments = json.getString("comments");      
       
       ImButtonDto ibDto = new ImButtonDto();
       ibDto.setButtonId(buttonId);
       ibDto.setButtonName(buttonName);
       ibDto.setButtonCode(buttonCode);
       ibDto.setComments(comments);
       
       try {
           this.ibService.modifyImButton(ibDto);
       } catch (Exception e) {
           logger.error("modify IM_BUTTON occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,ibDto);
	}
	
	/*
	 * hxz 按钮删除
	 * */
	@RequestMapping(value = "/imbutton/del",method = {RequestMethod.POST})
	public String imButtonDel(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imbutton--del-----");
        JSONObject json = JSONObject.fromObject(requestBody);
       
        Long buttonId = json.getLong("buttonId");
        
       
       ImButtonDto ibDto = new ImButtonDto();
       ibDto.setButtonId(buttonId);
       
       
       try {
           this.ibService.delImButton(ibDto);
       } catch (Exception e) {
           logger.error("del IM_BUTTON occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,ibDto);
	}
}
